package com.mf.all.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.all.entity.dto.DtoRolePermission;
import com.mf.all.entity.req.basic.ReqPermissionByRole;
import com.mf.all.mapper.MapperRolePermission;
import com.mf.all.service.ServiceRolePermission;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: lmf
 * @Create: 2025/4/4 14:23
 * @module
 */
@Service
public class ImplServiceRolePermission extends ServiceImpl<MapperRolePermission, DtoRolePermission> implements ServiceRolePermission {
    @Resource
    private MapperRolePermission mapperRolePermission;

    /**
     * 角色绑定权限
     */
    @Override
    @Transactional
    public void permissionByRole(ReqPermissionByRole reqPermissionByRole) {
        // 删除角色下绑定的权限
        final LambdaQueryWrapper<DtoRolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtoRolePermission::getRoleId, reqPermissionByRole.getRoleId());
        this.remove(wrapper);
        // 重新绑定角色下的权限
        if (!reqPermissionByRole.getPermissionIds().isEmpty()) {
            mapperRolePermission.permissionByRole(reqPermissionByRole);
        }
    }


    /**
     * 根据权限id查询是否绑定角色
     */
    @Override
    public boolean isBindRole(Integer permissionId) {
        final LambdaQueryWrapper<DtoRolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtoRolePermission::getPermissionId, permissionId);
        return this.count(wrapper) > 0;
    }
}
